NSPredicate Cheatsheet
Presented by Realm: a mobile database that replaces Core Data and SQLite. Learn more at http://realm.io
supported by Realm


Format String Summary
@"attributeName == %@"
object’s attribute name is equal to value passed in
@"%K == %@"
pass a string variable to %K, it will be represented as a keypath
@"%name IN $NAME_LIST"
templated for predicate, checks if the value of key name is in $NAME_LIST. Uses predicateWithSubstitutionVariables
                @"'name' IN $NAME_LIST"
                checks if the constant value ‘name’ is in $NAME_LIST. Uses predicateWithSubstitutionVariables
                predicateWithFormat: @"title == %@", @"minecraft"
                
                
                Keypath collection queries
                Basic Comparisons
                @avg
                @count
                @min
                @max
                returns the average of the objects in the collection as an NSNumber
                returns the number of objects in a collection as an NSNumber
                returns the minimum value of the objects in the collection as an NSNumber
                returns the maximum value of the objects in the collection as an NSNumber
                Basic Compound Predicates
                AND,&& Logical AND OR,|| Logical OR
                NOT,! Logical NOT
                predicateWithFormat: @"age == 40 AND price > 67"
                String Comparison Operators
                returns the sum of the objects in the collection @sum based on the property
                predicateWithFormat: @"expenses.@avg.doubleValue < 200"
    
                
                
                Subqueries
                SUBQUERY(collection, variableName, predicateFormat)
                Iterates through the collection to return qualifying queries
                Collection - array or set of objects
                variableName - variable that represents an iterated object
                predicateFormat - predicate that runs using the variableName
                predicateWithFormat: @"SUBQUERY(tasks, $task, $task.completionDate != nil AND $task.user = 'Alex') .@count > 0"
                Assume this was run on an array of projects. It will return projects with tasks that were not completed by user Alex
                
                =,== >=,=>
                <=,=<
                !=,<> Left hand expression is not equal to right hand expression
                Left hand expression must appear in collection specified by
                IN right hand expression. i.e. name IN {‘Milk’, ‘Eggs’, ‘Bread’} Leftft hand expression is between or equal to right hand
                BETWEEN expression. i.e. 1 Between {0, 33} predicateWithFormat: @"expenses BETWEEN {200, 400}
